<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="检测人" prop="inspector">
        <el-input
          v-model="queryParams.inspector"
          placeholder="请输入检测人"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库单号" prop="storeInId">
        <el-input
          v-model="queryParams.storeInId"
          placeholder="请输入入库单号"
          clearable
          style="width: 240px"
          @input="validateInputValue"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="rowList">
      <el-table-column label="检测单号" prop="id" align="center" min-width="120" fixed="left"/>
      <el-table-column label="检测时间" align="center" prop="checkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检测人" prop="inspector" align="center" width="120"/>
      <el-table-column label="入库单号" prop="storeInId" align="center" width="120">
        <template slot-scope="scope">
          <el-link type="primary" @click="openStoreIn(scope.row.storeInId)">{{ scope.row.storeInId }}</el-link>
<!--          <el-button size="mini" type="text"  @click="handleUpdate(scope.row)"></el-button>-->
        </template>
      </el-table-column>
      <el-table-column label="检测结果" prop="status" align="center" width="120">
        <template slot-scope="scope">
          <span :style="{'color': statusColors(scope.row.status)}">{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" align="center" width="120"/>
      <el-table-column label="创建人" prop="createBy"  width="120" align="center"/>
      <el-table-column label="修改人" prop="updateBy" width="120" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120" fixed="right"  class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"

          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改食材配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="检测时间" prop="checkTime">
              <el-date-picker v-model="form.checkTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                              :style="{width: '100%'}" placeholder="请选择检测时间" clearable></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检测人" prop="inspector">
              <el-input  v-model="form.inspector" placeholder="请输入检测人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { get, update ,list} from "@/api/examine/in/storeExamine";
export default {
  name: "In",
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 食材表格数据
      rowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeInId:'',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    validateInputValue() {
      this.queryParams.storeInId = this.queryParams.storeInId.replace(/[^0-9]/g, ""); // 只允许数字和小数点
    },
    statusColors(status) {
      if (status === "检测通过") {
        return '#2a9c4a'
      } else if (status === "检测不通过"){
        return '#FF0000'
      } else {
        return '#fe9f29'
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.rowList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    handleDetail: function(row) {
      const id = row.id;
      this.$router.push("/examine/in/detail/"+id);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改入库检测单";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      update(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    openStoreIn(id){
      this.$router.push("/store/in/detail/" + id);
      // this.$router.push({
      //   path:'store/in/detail/',
      //   query:id
      // })
    },
  }
};
</script>
